package com.wkk.util;


import com.wkk.entity.CoachPerformance;
import com.wkk.entity.Income;
import com.wkk.entity.IncomeOneDay;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


//使用SimpleDateFormat的parse()方法生成Date
//使用SimpleDateFormat的format()方法生成String

public class GeneralCalculation {

    //获取某年某月的天数
    public int getDayOfMonth(int year,int month){
        Calendar c = Calendar.getInstance();
        c.set(year, month, 0); //输入类型为int类型
        return c.get(Calendar.DAY_OF_MONTH);
    }

    //实现赋值操作
    public void assignmentIncome(List<Income> incomes,IncomeOneDay incomeOneDay){
        for(Income income:incomes){
            if(income.getType().equals("广告收入")){
                incomeOneDay.setAdvertisement(income.getMoney());
            }
            if(income.getType().equals("私教课收入")){
                incomeOneDay.setPrivateLesson(income.getMoney());
            }
            if(income.getType().equals("会员卡收入")){
                incomeOneDay.setMemberCard(income.getMoney());
            }
            if(income.getType().equals("特色课收入")){
                incomeOneDay.setFeaturedLesson(income.getMoney());
            }
            if(income.getType().equals("康复课收入")){
                incomeOneDay.setRehabilitationLesson(income.getMoney());
            }
        }
    }

    //获取某个月的最后一天
    public String getLastDayOfMonth(Integer month) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return simpleDateFormat.format(calendar.getTime());
    }

    //获取某月的第一天
    public String getFirstDayOfMonth(Integer month){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar= Calendar.getInstance();
        calendar.set(Calendar.MONTH, month);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        return simpleDateFormat.format(calendar.getTime());
    }

    //获取过去N天的日期数组
    public List<String> getLastDays(int dayCount){
       List<String> lastDays = new ArrayList<>();
        for (int i = 0; i < dayCount; i++) {
            lastDays.add(getPastDate(i));
        }
        return lastDays;
    }

    //获取过去第N天的日期(出发点是今天)
    public String getPastDate(int past) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        return simpleDateFormat.format(today);
    }

    //初始化值，防止出现异常
    public List<Integer> initList(List<Integer> list){
        list.add(0);
        list.add(0);
        list.add(0);
        list.add(0);
        list.add(0);
        list.add(0);
        list.add(0);
        return list;
    }

    //用于获取计算后的时间
    public String getMemberTime(Integer cid){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int days = 0;
        //此时的cid==card.title
        if(cid==1){
            days = 31;//月卡
        }
        if(cid==2){
            days = 124;//学期卡
        }
        if(cid==3){
            days = 186;//半年卡
        }
        if(cid==4){
            days = 365;//年卡
        }
        if(cid==5){
            days = 730;//两年卡
        }
        if(cid==6){
            days = 3660;//永久卡
        }
        Calendar calendar = Calendar.getInstance();
        calendar.add(calendar.DATE,days);
        Date date = calendar.getTime();
        return format.format(date);
    }

    //判断与当前时间的大小
    public int judgeTime(String endMember){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
         Date now = null;
         Date end = null;
        try{
            now = format.parse(format.format(new Date()));
            end = format.parse(endMember);
        }
          catch (ParseException e) {
            e.printStackTrace();
        }
        return now.compareTo(end);
    }

    //判断与当前时间的大小
    public int judgeDate(String date){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date nowDate = null;
        Date endDate = null;
        try{
            nowDate = format.parse(format.format(new Date()));
            endDate = format.parse(date);
        }
        catch (ParseException e) {
            e.printStackTrace();
        }
        return nowDate.compareTo(endDate);
    }


    public List<CoachPerformance> getPerforances(Integer cid){
        List<CoachPerformance> coachPerformances = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        Integer nowMonth = calendar.get(Calendar.MONTH) + 1;//获取当前月份
        Integer nowYear = calendar.get(Calendar.YEAR);//获取当前年份

        for(int i = nowMonth;i>0;i--){
            CoachPerformance performance = new CoachPerformance(cid,i,nowYear);
            coachPerformances.add(performance);
            if(coachPerformances.size()>=8){
                break;
            }
        }
        int size = coachPerformances.size();
        if(size<8){
            nowMonth = 12;
            nowYear = nowYear-1;
            for(int i = nowMonth;i>0;i--){
                CoachPerformance performance = new CoachPerformance(cid,i,nowYear);
                coachPerformances.add(performance);
                if(coachPerformances.size()>=8){
                    break;
                }
            }

        }
        return coachPerformances;
    }

}
